<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 列表管理
 */
namespace app\api\model;

use  app\api\model\Model;

class Withdraw extends Model{
	
    private $account = 'user_account'; // 账户表
    private $cert = 'user_cert'; // 账户表
    private $withdraw = 'user_withdraw'; // 兑换表

    /**
     * 提现列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //列表
        $list['datalist']= Model::name($this->withdraw)
            ->alias('a')
            ->join($this->account.' b','a.aid=b.aid','left')
            ->field('a.*,b.realname,b.username')
            ->limit(($page-1)*$page_size,$page_size)
            ->where($where)
            ->order('a.id desc')
            ->select()
            ->toArray();
        foreach ($list['datalist'] as $key => $value) {
          
        }
        //数据总数
        $list['total']=Model::name($this->withdraw)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $list;
    }

    /**
     * 提现信息
     * @param array $withdraw 
     * @param array $where 
     */ 
    public function withdraw($id=null){  
        $info=Model::name($this->withdraw)
           ->alias('a')
           ->join($this->account.' b','a.aid=b.aid','left')
           ->join($this->cert.' c','a.aid=c.aid','left')
           ->field('a.*, b.realname,b.mobile,b.username,b.email, c.bankaccount')
           ->where(['a.id'=>$id])
           ->find();
        if(isset($info['bankaccount'])){
            $info['bankaccount']=!empty($info['bankaccount'])&&isJson($info['bankaccount'])?json_decode($info['bankaccount'],true):[];
        }
        return $info;
    }

    /**
     * 提现保存
     * @param array $save 
     * @param array $where 
    */ 
    public function save($data){
        //保存
        $data  = [
            'aid'=>$data['aid'],
            'pid'=>$data['pid'],
            'payment'=>$data['payment'],
            'money'=>$data['money'],
            'remoney'=>round($data['money']*$data['rate']*100)/100,
            'integra'=>$data['integra'],
            'reintegra'=>round($data['integra']*$data['rate']*100)/100,
            'rate'=>$data['rate'],
            'codes'=>$data['codes'],
            'currency'=>$data['currency'],
            'fee'=>$data['fee'],
            'createtime'=>date('Y-m-d H:i:s',time())
        ];
        $info=Model::name($this->withdraw)->insert($data);
        return $info;
    }

}
